package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 修改密码
 * 
 * @author zk
 *
 */
public class NotifyPwdActivity extends BaseActivity {

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	/** 原密码 */
	private EditText notify_old_pwd;
	/** 新密码 */
	private EditText notify_new_pwd;
	/** 再次密码 */
	private EditText notify_sure_pwd;
	/** 修改 */
	private Button notify_pwd_btn;

	@Override
	public int layoutResID() {
		return R.layout.activity_notify_pwd;
	}

	@Override
	public void findView() {
		findTitleIds();

		notify_old_pwd = V.f(this, R.id.notify_old_pwd);
		notify_new_pwd = V.f(this, R.id.notify_new_pwd);
		notify_sure_pwd = V.f(this, R.id.notify_sure_pwd);
		notify_pwd_btn = V.f(this, R.id.notify_pwd_btn);
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.baseduobao_right_tv);

		base_title_tv.setText("修改密码");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		notify_pwd_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		String old_pwd = notify_old_pwd.getText().toString();
		String new_pwd = notify_new_pwd.getText().toString();
		String repwd = notify_sure_pwd.getText().toString();
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		case R.id.notify_pwd_btn:// 确定
			if(TextUtils.isEmpty(old_pwd)){
				showToast("请输入原登录密码");
				return ;
			}
			if(TextUtils.isEmpty(new_pwd)){
				showToast("请输入新的登录密码");
				return ;
			}
			if(TextUtils.isEmpty(repwd)){
				showToast("请再输入一次新的登录密码");
				return ;
			}
			if(!new_pwd.equals(repwd)){
				showToast("您两次输入的新密码不正确");
				return ;
			}
			notifyPwd(old_pwd,new_pwd,repwd);
			break;
		}
	}

	private void notifyPwd(String old_pwd, String new_pwd, String repwd) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		JSONObject info = new JSONObject();
		try {
			info.put("pwd", old_pwd);
			info.put("newpwd", new_pwd);
			info.put("repwd", repwd);
			info.put("uid", PrefUtils.getString(this, "userId", ""));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			json.put("model", "Members");
			json.put("function", "UpPwdApp");
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, Values.BASE_URL + "Appport/appMemberInfo", params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if(jso.getInt("status") == 1){
						showToast("修改成功！");
						finish();
					}else{
						showToast(jso.getString("data"));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}


}
